package com.fy.common.util;

import com.fy.common.constant.RedisKeyConstant;

/**
 * @author zhshuo
 * @date 2021/12/13 11:25
 */
public final class ImUtil {
    /**
     * 私聊
     */
    public static final String FRIEND_CHAT_SESSION_PREFIX = "cs-p-";
    /**
     * 群聊
     */
    public static final String GROUP_CHAT_SESSION_PREFIX = "cs-g-";

    /**
     * 好友群聊 会话
     *
     * @param mainUserId 用户ID
     * @param targetId   目标用户或者群组ID
     * @return 会话ID
     */
    public static String chatSessionIdGenFriend(Long mainUserId, Long targetId) {
        return chatSessionIdGen(FRIEND_CHAT_SESSION_PREFIX, mainUserId, targetId);
    }

    /**
     * 群聊会话
     *
     * @param mainUserId 用户ID
     * @param targetId   目标用户或者群组ID
     * @return 会话ID
     */
    public static String chatSessionIdGenGroup(Long mainUserId, Long targetId) {
        return chatSessionIdGen(GROUP_CHAT_SESSION_PREFIX, mainUserId, targetId);
    }

    /**
     * 返回类似 {@code pig|USER_CHAT_SESSION_1468475282761461762} 这种
     * @param userId
     * @return
     */
    public static String chatSessionKeyGen(String userId){
        return RedisKeyConstant.USER_CHAT_SESSION+userId;
    }

    /**
     * @return 会话ID
     */
    private static String chatSessionIdGen(String prefix, Long mainUserId, Long targetId) {
        return new StringBuilder(prefix).append(mainUserId).append("-").append(targetId).toString();
    }

}
